package com.frank.demo.controller;/*
 *@author:xec
 *@date:2022/2/28  8:58
 */

import com.frank.demo.req.DocQueryReq;
import com.frank.demo.req.DocSaveReq;
import com.frank.demo.resp.DocQueryResp;
import com.frank.demo.resp.CommonResp;
import com.frank.demo.resp.PageResp;
import com.frank.demo.service.DocService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 *  对doc表的操作都在此接口下执行
 */

@RestController
@RequestMapping("/doc")
public class DocController {

    @Resource
    DocService docService;


    /**
     * 查询所有文档的方法
     * @return
     */
    @GetMapping("/getAllDoc")
    public CommonResp selectAllDoc(){
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> list = docService.getAll();
        resp.setContent(list);
        return resp;
    }

    /**
     * 根据电子书id查询文档的方法,结果已经排好序
     */
    @GetMapping("/getDocByEbookId/{ebookId}")
    public CommonResp all(@PathVariable Long ebookId){
        CommonResp<List<DocQueryResp>> resp = new CommonResp<List<DocQueryResp>>();
        List<DocQueryResp> list = docService.getDocByEbookId(ebookId);
        resp.setMessage("查询成功");
        resp.setContent(list);
        return  resp;
    }

    /**
     * 带条件查询文档
     * @param docQueryReq
     * @return
     * 注解 @Valid 开启规则检验
     */
    @GetMapping("/getDoc")
    public CommonResp<PageResp<DocQueryResp>> selectEBook(@Valid DocQueryReq docQueryReq){
        PageResp<DocQueryResp> list = docService.getDoc(docQueryReq);
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        resp.setMessage("查询成功");
        resp.setContent(list);
        return resp;
    }

    /**
     * 查询文档内容
     * @param id
     * @return
     */
    @GetMapping("/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id){
        CommonResp<String> resp = new CommonResp<>();
        String content = docService.findContent(id);
        resp.setContent(content);
        return resp;
    }

    /**
     * 保存方法
     * @param req
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@RequestBody DocSaveReq req) {
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        return resp;
    }

    /**
     * 根据id批量删除
     */
    @DeleteMapping("/delete/{idsStr}")
    public CommonResp delete(@PathVariable String idsStr) {
        List<String> stringList = Arrays.asList(idsStr.split(","));
        CommonResp resp = new CommonResp<>();
        docService.delete(stringList);
        return resp;
    }

    /**
     * 点赞接口
     * @param id
     * @return
     */
    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable Long id) {
        CommonResp commonResp = new CommonResp<>();
        docService.vote(id);
        return commonResp;
    }
}
